<?php
namespace app\admin\controller;
use think\Db;
use think\response\Json; //使用数据类库

class Blog extends Base
{
    // 博客内容列表
    public function blog_list()
    {
        //查询博客
        $blog = Db::name('blog')
            -> alias('b')
            -> field("b.*,c.category_name,a.admin_name")
            -> join("pre_categories c","b.category_id=c.category_id","left")
            -> join("pre_admin a","b.admin_id=a.admin_id","left")
            -> paginate(3);
        $current = $blog -> currentPage();
        $a = ($current -1) *5+1;
        $this -> assign("blog",$blog);
        $this -> assign("a",$a); //编号
        return $this -> fetch();
    }
    //博客内容添加
    public function blog_add()
    {
        //查询分类
        $cate = Db::name('categories')->select();
        //查询会员
        $admin = Db::name("admin")->select();
        //做添加功能
        if ($_POST) { //要记得判断是否提交
            $blog_time = !empty($_POST['blog_time']) ? strtotime($_POST['blog_time']) : time();
            $data = array(
                'blog_title' => $this->request->post('blog_title'),
                'blog_content' => $this->request->post('blog_content'),
                'blog_time' => $blog_time,
                'category_id' => $this->request->post('category_id'),
                'admin_id' => $this->request->post('admin_id'),
            );
            //执行添加功能
            $res = Db::name('blog')->insert($data);
            //判断结果并跳转页面
            if ($res) {
                $this->success("添加成功", "/admin/blog/blog_list");
            } else {
                $this->error("数据执行有误");
            }
        }
        $this->assign("cate", $cate);
        $this->assign("admin", $admin);
        return $this->fetch();
    }
    //博客内容编辑
    public function blog_edit($bid)
    {
        //查询分类
        $cate = Db::name('categories')->select();
        //查询会员
        $admin = Db::name("admin")->select();
        $this->assign("cate", $cate);
        $this->assign("admin", $admin);

        //接收 bid 查询当前编辑的数据
        $blog = Db::name('blog')-> where("blog_id", $bid) -> find();
        $blog_time = $blog['blog_time'];
        $blog_time = date('Y-m-d', $blog_time);
        $blog['blog_time'] = $blog_time;
        $this->assign("blog", $blog);
        //做编辑功能
        if ($_POST) {
            //接收隐藏域
            $blog_id = $this->request->post('blog_id');
            $blog_time = $this->request->post('blog_time');
            $data = array(
                'blog_title' => $this->request->post('blog_title'),
                'blog_content' => $this->request->post('blog_content'),
                'blog_time' => strtotime($blog_time),
                'category_id' => $this->request->post('category_id'),
                'admin_id' => $this->request->post('admin_id'),
            );
            //执行编辑功能
            $res = Db::name("blog")->where("blog_id", $blog_id)->update($data);
            //判断结果并跳转页面
            if ($res) {
                $this->success("编辑成功", "/admin/blog/blog_list");
            } else {
                $this->error("数据执行有误");
            }
        }
        return $this->fetch();
    }
    //博客内容删除
    public function blogdel($bid) {
        //执行删除语句
        $res = Db::name('blog') -> where("blog_id",$bid) -> delete();
        if($res) {
            //删除成功
            echo 1;exit();
        }else{
            echo Db::name('blog') -> getLastSql();
        }
    }
    //批量删除
    public function batch() {
        $blogarr = $this -> request -> post('id');
        $blogstr =  implode(",",$blogarr);
        $res = Db::name("blog") -> where("blog_id",$blogstr) -> delete();
        if ($res) {
            $this -> success("批量删除成功","/admin/blog/blog_list");
        }else {
            $this -> error("数据执行有误");
        }
    }
    //博客分类列表
    public function blogcat_list() {
        //查询分类
        $blogcat = Db::name('categories')->paginate(5);
        $current = $blogcat->currentPage();
        $a = ($current - 1) * 5 + 1;
        $this->assign("blogcat", $blogcat);
        $this->assign("a", $a); //编号
        return $this -> fetch();
    }
    //博客分类添加
    public function blogcat_add() {
        if ($_POST){
            $data=array(
                'category_name' => $this -> request -> post('category_name'),
            );
            //获取上传文件
            $file = request()->file('path'); //path是上传框的name值
            $info = $file -> move('uploads'); //上传到根目录 uploads文件夹内
            //必须先在根目录新建 uploads 文件夹
            if ($info){ //上传成功
                $path = $info -> getSaveName();
                $path = str_replace("\\","/",$path);
                $data['path'] = $path;
            }else{
                //上传不成功
                $this -> error($file -> getError());
            }
            //执行添加功能
            $res = Db::name('categories')->insert($data);
            //判断结果并跳转页面
            if ($res) {
                $this->success("添加成功", "/admin/blog/blogcat_list");
            } else {
                $this->error("数据执行有误");
            }

        }
        return $this -> fetch();
    }
    //博客分类编辑
    public function blogcat_edit($cid) {
        //查询当前数据
        $category =  Db::name("categories") -> where("category_id=$cid") -> find();
        $this -> assign("category",$category);
        if ($_POST) {
            $data = array(
                'category_name' => $this -> request -> post('category_name'),
            );
            //接收隐藏域
            $category_id = $this -> request -> post('category_id');
            if (request()->file()) {
                // 获取文件上传
                $file = request() ->file('path'); //path是上传框的name 值
                $info = $file -> move('uploads');//上传到目录的 uploads 文件夹内
                //必须先在根目录新建 uploads 文件夹
                if ($info){ //上传成功
                    //删除旧图片
                    $old_arr = Db::name('categories') -> field("path") -> where("category_id=$category_id") -> find();
                    $old_path = $old_arr['path'];
                    $all_path = './uploads/'.$old_path; //完整路径
                    if (!empty($old_path) && file_exists($all_path)) {
                        //判断不为空并且图片存在
                        unlink($all_path);
                    }
                    $path = $info -> getSaveName();
                    $path = str_replace("\\","/",$path);
                    $data['path'] = $path;
                }else{
                    //上传不成功
                    $this -> error($file -> getError());
                }
            }
            //执行编辑功能
            $res =  Db::name("categories") -> where("category_id=$category_id") -> update($data);
            if ($res) {
                $this->success("编辑成功", "/admin/blog/blogcat_list");
            } else {
                $this->error("数据执行有误");
            }
        }
        return $this -> fetch();
    }
    //博客分类删除
    public function blogcat_del($cid) {
        $res = Db::name('categories') -> where("category_id",$cid)->delete();
        if ($res) { //删除成功
            echo 1;exit();
        }else {
            echo Db::name('categories') -> getLastSql(); exit();
        }
        return $this -> fetch();
    }
    //分类批量操作
    public function batchacat() {
        $blogarr = $this -> request -> post('id');
        $blogstr = implode(",",$blogarr);
        //删除旧图片
        $old_arr = Db::name('categories') -> field("path") -> where("category_id=$blogstr") -> select();
        foreach ($old_arr as $v) {
            $old_path = $v['path'];
            $all_path = './uploads/'.$old_path; //完整路径
            if (!empty($old_path) && file_exists($all_path)) {
                //判断不为空并且图片存在
                unlink($all_path);
            }
        }
        $res = Db::name('categories') -> whereIn("category_id",$blogstr) -> delete();
        if ($res) {
            $this->success("批量删除成功", "/admin/blog/blogcat_list");
        } else {
            $this->error("数据执行有误");
        }
    }

    //文章评论管理
    public function reply_list() {
        $reply = Db::name('reply')
            -> alias('r')
            -> field("r.*,b.blog_title")
            -> join("pre_blog b","r.blog_id=b.blog_id","left")
            -> paginate(3);
        $current = $reply -> currentPage();
        $a = ($current -1) *3+1;

        $this -> assign("reply",$reply);
        $this -> assign("a",$a); //编号
        return $this -> fetch();
    }

    //评论添加
    public function reply_add()
    {
        //查询所属文章
        $blog = Db::name('blog') -> select();
        //做添加功能
        if ($_POST) { //要记得判断是否提交
            $reply_time = !empty($_POST['reply_time']) ? strtotime($_POST['reply_time']) : time();
            $data = array(
                'reply_email' => $this->request->post('reply_email'),
                'reply_time' =>  $reply_time,
                'reply_content' => $this->request->post('reply_content'),
                'blog_id' => $this->request->post('blog_id'),
            );
            //执行添加功能
            $res = Db::name('reply')->insert($data);
            //判断结果并跳转页面
            if ($res) {
                $this->success("添加成功", "/admin/blog/reply_list");
            } else {
                $this->error("数据执行有误");
            }
        }
        $this -> assign("blog",$blog);
        return $this->fetch();
    }

    //文章评论编辑
    public function reply_edit($bid)
    { //查询所属文章
        $blog = Db::name('blog') -> select();
        $this -> assign("blog",$blog);

        //接收 bid 查询当前编辑的数据
        $reply = Db::name('reply')-> where("reply_id", $bid) -> find();
        $reply_time = $reply['reply_time'];
        $reply_time = date('Y-m-d', $reply_time);
        $reply['reply_time'] = $reply_time;
        $this->assign("reply", $reply);
        //做编辑功能
        if ($_POST) {
            //接收隐藏域
            $reply_id = $this->request->post('reply_id');
            $reply_time = $this->request->post('reply_time');
            $data = array(
                'reply_email' => $this->request->post('reply_email'),
                'reply_time' => strtotime($reply_time),
                'reply_content' => $this->request->post('reply_content'),
                'blog_id' => $this->request->post('blog_id'),
            );
            //执行编辑功能
            $res = Db::name("reply")->where("reply_id", $reply_id)->update($data);
            //判断结果并跳转页面
            if ($res) {
                $this->success("编辑成功", "/admin/blog/reply_list");
            } else {
                $this->error("数据执行有误");
            }
        }
        return $this->fetch();
    }

    //文章评论删除
    public function reply_del($bid) {
        $res = Db::name('reply') -> where("reply_id",$bid)->delete();
        if ($res) { //删除成功
            echo 1;exit();
        }else {
            echo Db::name('reply') -> getLastSql(); exit();
        }
        return $this -> fetch();
    }

    //文章评论批量操作
    public function batchareply() {
        $blogarr = $this -> request -> post('id');
        $blogstr =  implode(",",$blogarr);
        $res = Db::name("reply") -> where("reply_id",$blogstr) -> delete();
        if ($res) {
            $this -> success("批量删除成功","/admin/blog/reply_list");
        }else {
            $this -> error("数据执行有误");
        }
    }
}